import Taro from "@tarojs/taro";
import { BaseOptionsType, ParamType } from "../models/requestType";

const baseURL ="https://cookserver.wingosite.site/api"

/**
 * @description 封装Taro.request
 * @author Bronson
 * @date 2022-05-16
 * */
function request<T extends object> ({ params , method = "GET" }:BaseOptionsType):Promise<T>{
  return new Promise((resolve,reject)=>{
    Taro.request({
      url:params.url.indexOf("http") === -1 ? baseURL + params.url : params.url,
      data:params.data,
      dataType:"json",
      method:method,
      header:{
        "Content-Type": "application/x-www-form-urlencoded",
        "token":"123"
      },
      success:res=>{
        if(res.statusCode === 200){
          resolve(res.data)
        }else{
          reject(res.errMsg)
        }
      },
      fail:err=>{
        Taro.hideLoading()
        reject(err)
      }
    })
  })
}


request.get = <T extends object>(params:ParamType):Promise<T> => {
  return request({params,method:"GET"})
}

request.post = <T extends object>(params:ParamType):Promise<T> => {
  return request({params,method:"POST"})
}


export default request
